//
//  ForgetPassWordVController.m
//  Experss
//
//  Created by 嗯，大葱 on 17/8/9.
//  Copyright © 2017年 嗯，大葱. All rights reserved.
//

#import "ForgetPassWordVController.h"
#import "NSString+Verify.h"

@interface ForgetPassWordVController ()
{
    NSTimer *timer;
//    NSString *yanZhengM;
    NSInteger time;
    

}
@end

@implementation ForgetPassWordVController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configureNaBar];
    
    _yanZBtn.hidden = NO;
    _timeLB.hidden = YES;
    
    
}
- (void)configureNaBar {
    self.navigationItem.title = @"找回密码";
    //设置导航栏按钮的边距
    UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame= CGRectMake(0, 0, 60, 30);
    [rightBtn setImage:[UIImage imageNamed:@"all_fanhui2"] forState:(UIControlStateNormal)];
    [rightBtn addTarget:self action:@selector(handleBackAxtion) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *btn_left = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    UIBarButtonItem *negativeSpacerleft = [[UIBarButtonItem alloc]   initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace   target:nil action:nil];
    /**
     width为负数时，相当于btn向右移动width数值个像素，由于按钮本身和  边界间距为5pix，所以width设为-5时，间距正好调整为0；width为正数 时，正好相反，相当于往左移动width数值个像素
     */
    negativeSpacerleft.width = -20;
    self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacerleft, btn_left, nil];
    
    
    
}
- (void)handleBackAxtion {
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

//验证码按钮点击事件
- (IBAction)yanzhengMBtnAction:(UIButton *)sender {
    
    if (![_phoneTF.text verifyTelephone]) {
        
        [self.view showTextNoActivity:@"手机格式不正确" timeLength:1];
        return;
    }
    
    [self getCodeStr];
    
    __block int timeout=59; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0){ //倒计时结束，关闭
            
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                
                _yanZBtn.hidden = NO;
                _timeLB.hidden = YES;;
                
            });
        }else{
            int seconds = timeout % 60;
            NSString *strTime = [NSString stringWithFormat:@"%.2dS", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:1];
                _yanZBtn.hidden = YES;
                _timeLB.hidden = NO;
                _timeLB.text = strTime;
                [UIView commitAnimations];
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
}
// 获取验证码
-(void)getCodeStr{
    NSDictionary *parameDic = @{@"mobile":_phoneTF.text,@"actoken":[Md5Apiway getMD5data:@""]};
    if ([_phoneTF.text verifyTelephone]) {
        [HttpRequest postWithURLString:POST(Shop_GetSmsCodeUrl) parameters:parameDic success:^(id responseObject) {
            
            NSLog(@"%@",responseObject);
            NSDictionary *dataDIc = [responseObject objectForKey:@"data"];
            NSString *codeStr = [NSString stringWithFormat:@"%@",[dataDIc objectForKey:@"code"]];
            if (!dataDIc.count) {
                [MBProgressHUD showText:[NSString stringWithFormat:@"%@",[responseObject objectForKey:@"msg"]]];
                return ;
            }
            if (!(codeStr.integerValue == 0)) {
                [MBProgressHUD showText:[NSString stringWithFormat:@"%@",[dataDIc objectForKey:@"msg"]]];
            }
            
        } failure:^(NSError *error) {
            
        }];
    }
    
}


//确认按钮的点击事件
- (IBAction)sureBtnAction:(UIButton *)sender {
    
    if (_passTF.text.length && _yanzhengMTF.text.length==6) {
        NSDictionary *parameDic = @{@"sms_code":_yanzhengMTF.text,@"mobile":_phoneTF.text,@"newpassword":_passTF.text};
        
        [HttpRequest postWithURLString:POST(Shop_RetrievalPass) parameters:parameDic success:^(id responseObject) {
            NSLog(@"%@",responseObject);
            
            NSDictionary *dataDic = [responseObject objectForKey:@"data"];
            if (dataDic.count) {
               NSString *codeStr = [NSString stringWithFormat:@"%@",[dataDic objectForKey:@"code"]];
                if (codeStr.integerValue == 0) {
                    
                    [MBProgressHUD showText:@"修改成功!"];
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        [self.navigationController popViewControllerAnimated:YES];
                    });
                    
                    
                }else{
                    [MBProgressHUD showText:[NSString stringWithFormat:@"%@",[dataDic objectForKey:@"msg"]]];
                }
                
            }else{
            [MBProgressHUD showText:[NSString stringWithFormat:@"%@",[responseObject objectForKey:@"msg"]]];
            
            }
            
        } failure:^(NSError *error) {
            
        }];

    }else{
        [MBProgressHUD showText:@"请输入正确信息!"];
    }
    
}


@end
